package com.ruoyi.project.warehouseSystem.controller;

import java.util.List;
import java.util.Random;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.warehouseSystem.domain.CWarehouse;
import com.ruoyi.project.warehouseSystem.service.ICWarehouseService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 仓库的增删改查Controller
 * 
 * @author wangshuo
 * @date 2021-07-18
 */
@RestController
@RequestMapping("/warehouseSystem/configuration")
public class CWarehouseController extends BaseController
{
    @Autowired
    private ICWarehouseService cWarehouseService;

    /**
     * 查询仓库的增删改查列表
     */
    @PreAuthorize("@ss.hasPermi('warehouseSystem:configuration:list')")
    @GetMapping("/list")
    public TableDataInfo list(CWarehouse cWarehouse)
    {
        startPage();
        List<CWarehouse> list = cWarehouseService.selectCWarehouseList(cWarehouse);
        return getDataTable(list);
    }

    /**
     * 导出仓库的增删改查列表
     */
    @PreAuthorize("@ss.hasPermi('warehouseSystem:configuration:export')")
    @Log(title = "仓库的增删改查", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(CWarehouse cWarehouse)
    {
        List<CWarehouse> list = cWarehouseService.selectCWarehouseList(cWarehouse);
        ExcelUtil<CWarehouse> util = new ExcelUtil<CWarehouse>(CWarehouse.class);
        return util.exportExcel(list, "configuration");
    }

    /**
     * 获取仓库的增删改查详细信息
     */
    @PreAuthorize("@ss.hasPermi('warehouseSystem:configuration:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(cWarehouseService.selectCWarehouseById(id));
    }

    /**
     * 新增仓库的增删改查
     */
    @PreAuthorize("@ss.hasPermi('warehouseSystem:configuration:add')")
    @Log(title = "仓库的增删改查", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CWarehouse cWarehouse)
    {
        Random random = new Random();
        String result="";
        for (int i=0;i<6;i++)
        {
            result+=random.nextInt(10);
        }
        System.out.println(result);
        String cknumber="ckbh"+result;//按照规则组合，前缀+6位随机生成的编号作为仓库编号
        cWarehouse.setwNumber(cknumber);
//        int i=cWarehouseService.insertCWarehouse(cWarehouse);
        return toAjax(cWarehouseService.insertCWarehouse(cWarehouse));
    }

    /**
     * 修改仓库的增删改查
     */
    @PreAuthorize("@ss.hasPermi('warehouseSystem:configuration:edit')")
    @Log(title = "仓库的增删改查", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CWarehouse cWarehouse)
    {
        return toAjax(cWarehouseService.updateCWarehouse(cWarehouse));
    }

    /**
     * 删除仓库的增删改查
     */
    @PreAuthorize("@ss.hasPermi('warehouseSystem:configuration:remove')")
    @Log(title = "仓库的增删改查", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(cWarehouseService.deleteCWarehouseByIds(ids));
    }
}
